package com.ywjt.platformbusiness.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.my.adapter.IntegralAdapter;
import com.ywjt.platformbusiness.my.model.IntegralModel;
import com.ywjt.platformbusiness.my.model.MyCouponListModel;
import com.ywjt.platformbusiness.util.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyIntegralActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    private RecyclerView rcCommon;
    private IntegralAdapter integralAdapter;
    private SmartRefreshLayout refresh;
    private TextView tvGoldIntegral,tvSliverIntegral;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyIntegralActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(false);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_myintegral;
    }

    @Override
    protected void initView() {
        ivBack=findViewById(R.id.ivBack);
        refresh=findViewById(R.id.refresh);
        tvSliverIntegral=findViewById(R.id.tvSliverIntegral);
        ivBack.setOnClickListener(this::onClick);
        rcCommon=findViewById(R.id.rcCommon);
        tvGoldIntegral=findViewById(R.id.tvGoldIntegral);


        refresh.finishRefreshWithNoMoreData();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });

        getData();
    }

    @Override
    protected void initData() { }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    //积分流水
    public void getData(){
        new HttpRequest(getContext()).doGet(UrlConstants.getUserIntegralMessage, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        refresh.finishRefresh();
                        Object obj = JsonUtils.fromJson(result.toString(), IntegralModel.class);
                        if (obj instanceof IntegralModel) {
                            IntegralModel integralModel =(IntegralModel)obj;

                            tvGoldIntegral.setText(integralModel.getResult().getGoldIntegral().intValue()+"");
                            tvSliverIntegral.setText(integralModel.getResult().getSilverIntegral().intValue()+"");

                            if (integralModel.getResult().getIntegralFlowList()!=null&&
                                    integralModel.getResult().getIntegralFlowList().size() > 0) {
                                /*****/
                                integralAdapter=new IntegralAdapter(integralModel.getResult().getIntegralFlowList());
                                rcCommon.setLayoutManager(new LinearLayoutManager(getContext()));
                                rcCommon.setAdapter(integralAdapter);
                            } else {
                                refresh.finishRefreshWithNoMoreData();
                            }

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }
}